package com.byt.framlib.commonutils.notifycation;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
/**
 * \使用
 * BasicNotificationBuilder builder = new BasicNotificationBuilder(mBContext, new BasicNotificationBuilder.GetPendingIntent() {
 *
 * @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 * @Override public PendingIntent getPendingIntent() {
 * Intent intent = new Intent(mBContext , Test2.class);
 * <p>
 * TaskStackBuilder stackBuilder = TaskStackBuilder.create(mBContext);
 * // 添加后台堆栈
 * stackBuilder.addParentStack(Test2.class);
 * // 添加Intent到栈顶
 * stackBuilder.addNextIntent(intent);
 * // 获得一个PendingIntent包含整个后台堆栈 containing the entire back stack
 * PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
 * return pendingIntent;
 * }
 * },);
 * MyNotificationManager.getInstanse().setNotificationInterface(builder);
 * MyNotificationManager.getInstanse().sendNotification(mBContext, "通知标题", "通知内容", R.drawable.push_small);
 */

/**
 * MyNotificationManager
 * Notificatoin 管理器
 */
public class MyNotificationManager {
    private NotificationInterface mNotificationInterface;
    private static MyNotificationManager instanse;
    private NotificationManager notificationManager = null;
    private static final int START_ID = 1000;
    private static final int RANGE = 50;
    private int mCurrentId = START_ID;

    private MyNotificationManager() {
    }

    public static MyNotificationManager getInstanse() {
        if (instanse == null) {
            synchronized (MyNotificationManager.class) {
                if (instanse == null) {
                    instanse = new MyNotificationManager();
                }
            }
        }
        return instanse;
    }

    public NotificationInterface getNotificationInterface() {
        return mNotificationInterface;
    }

    public void setNotificationInterface(NotificationInterface notificationInterface) {
        mNotificationInterface = notificationInterface;
    }

    /**@description 对外发送通知的接口
     * @param context
     * @param title  标题
     * @param content  内容
     * @param iconId  图标 id
     */
    public void sendNotification(Context context, String title, String content, int iconId) {
        buildAndDisplayNotification(context, title, content, iconId);
    }

    /**@description 创建通知
     * @param context
     * @param title  标题
     * @param content  内容
     * @param iconId  图标 id
     */
    private void buildAndDisplayNotification(Context context, String title, String content, int iconId) {
        if (mNotificationInterface != null) {
            Notification notification = mNotificationInterface.generateNotifacationBuilder(title, content, iconId).build();
            if (notification != null) {
                if (notificationManager == null) {
                    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                }
                notificationManager.notify(generateNotification(), notification);
            }
        }
    }

    /**
     * @return Notification 的id
     */
    private int generateNotification() {
        mCurrentId++;
        if (mCurrentId >= START_ID + RANGE) {
            mCurrentId = START_ID;
        }
        return mCurrentId;
    }

}
